import { defineStore } from 'pinia'

// 主题枚举
export const THEME = {
  LIGHT: 'light',
  DARK: 'dark',
  AUTO: 'auto'
}

// 主题状态管理模块
export const useThemeStore = defineStore('theme', {
  state: () => ({
    // 当前主题
    theme: THEME.AUTO,
    // 系统主题偏好
    systemTheme: THEME.LIGHT
  }),
  
  getters: {
    // 获取当前主题
    currentTheme: (state) => {
      if (state.theme === THEME.AUTO) {
        return state.systemTheme
      }
      return state.theme
    },
    // 是否为暗色主题
    isDarkTheme: (state) => {
      const current = state.currentTheme
      return current === THEME.DARK
    }
  },
  
  actions: {
    // 设置主题
    setTheme(theme) {
      this.theme = theme
    },
    
    // 设置系统主题偏好
    setSystemTheme(systemTheme) {
      this.systemTheme = systemTheme
    },
    
    // 切换主题
    toggleTheme() {
      const themes = [THEME.LIGHT, THEME.DARK, THEME.AUTO]
      const currentIndex = themes.indexOf(this.theme)
      const nextIndex = (currentIndex + 1) % themes.length
      this.theme = themes[nextIndex]
    }
  },
  
  // 持久化配置
  persist: {
    key: 'theme-store',
    storage: localStorage,
    paths: ['theme']
  }
})